package cn.zwx.segment.dao;

import cn.zwx.segment.model.LeafAlloc;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zhangwenxue
 */
public interface IDAllocMapper {

    /**
     *  获取所有leaf配置
     * @return List<LeafAlloc>
     */
    @Select("SELECT biz_tag, max_id, step, update_time FROM leaf_alloc")
    @Results(value = {
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<LeafAlloc> getAllLeafAllocs();

    /**
     *  根据Tag更 leaf配置
     * @param tag tag
     * @return LeafAlloc
     */
    @Select("SELECT biz_tag, max_id, step FROM leaf_alloc WHERE biz_tag = #{tag}")
    @Results(value = {
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step")
    })
    LeafAlloc getLeafAlloc(@Param("tag") String tag);

    /**
     *  根据tag更新MaxId
     * @param  tag 更新配置请求参数
     */
    @Update("UPDATE leaf_alloc SET max_id = max_id + step WHERE biz_tag = #{tag}")
    void updateMaxId(@Param("tag") String tag);
    /**
     *  更新LeafAlloc配置
     * @param  leafAlloc 更新配置请求参数
     * @return LeafAlloc
     */
    @Update("UPDATE leaf_alloc SET max_id = max_id + #{step} WHERE biz_tag = #{key}")
    void updateMaxIdByCustomStep(@Param("leafAlloc") LeafAlloc leafAlloc);

    /**
     *  getAllTags 获取所有tag
     * @return List<String>
     */
    @Select("SELECT biz_tag FROM leaf_alloc")
    List<String> getAllTags();

}
